﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MainScene;
using MindFusion.Diagramming.WinForms;
using MindFusion.Diagramming;

namespace SSShell
{
    public partial class AddChangeNode : Form
    {
        frmMain formMain;
        ClassNode Selected_Node;
        DiagramNode Selected_DNode;
        Boolean is_redacted = false;
        TypeNode old_type;
        float X, Y;
        public AddChangeNode(frmMain _formMain, Boolean redacted, ClassNode _selnode, DiagramNode _selDNode, float x, float y)
        {
            InitializeComponent();
            formMain = _formMain;
            is_redacted = redacted;
            Selected_Node = _selnode;
            Selected_DNode = _selDNode;
            if (Selected_Node!=null)
                old_type = Selected_Node.TypeN;
            Y = y; 
            X = x;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox1.SelectedIndex == 0)
            {
                groupBox1.Visible = true;
                groupBox2.Visible = false;
            }
            else
            {
                groupBox2.Visible = true;
                groupBox1.Visible = false;
                cmbAction.Items.Clear();
                //cmbAgent.Items.Clear();
                //cmbRec.Items.Clear();
                //foreach (TreeNode item in formMain.treeViewTaxonomia.Nodes)
                //{
                    
                //    //cmbAgent.Items.Add(item.Text);
                //    //cmbRec.Items.Add(item.Text);
                //}
                foreach (string item in formMain.SS.LstActions)
                {
                    cmbAction.Items.Add(item);
                }
                //foreach (string item in formMain.SS.LstAgents)
                //{
                //    cmbAgent.Items.Add(item);
                //}
                //foreach (string item in formMain.SS.LstRecipients)
                //{
                //    cmbRec.Items.Add(item);
                //}
                foreach (string item in formMain.SS.LstLocotivs)
                {
                    cmbLoc.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstObjs)
                {
                    cmbObj.Items.Add(item);
                }
                foreach (string item in formMain.SS.LstTimes)
                {
                    cmbTime.Items.Add(item);
                }
            }
        }

        private void AddChangeNode_Load(object sender, EventArgs e)
        {
            foreach (TreeNode item in formMain.treeViewTaxonomia.Nodes)
            {
                treeViewAg.Nodes.Add(item.Text);
                treeViewRec.Nodes.Add(item.Text);
            }
            treeViewAg.ExpandAll();
            treeViewRec.ExpandAll();
            if (!is_redacted)
                comboBox1.SelectedIndex = 0;
            else
            {
                if (Selected_Node.TypeN == TypeNode.Action)
                {
                    comboBox1.SelectedIndex = 1;
                    //cmbAgent.Text = Selected_Node.CaseFrame.Agent.Name;
                    //cmbRec.Text = Selected_Node.CaseFrame.Recipient.Name;
                    //txtAction.Text = Selected_Node.CaseFrame.ToDo.Name;
                    txtAgent.Text = Selected_Node.CaseFrame.Agent.Name;
                    txtRec.Text = Selected_Node.CaseFrame.Recipient.Name;
                    cmbAction.Text = Selected_Node.CaseFrame.ToDo.Name;
                    cmbLoc.Text = Selected_Node.CaseFrame.Lokotiv.Name;
                    cmbTime.Text = Selected_Node.CaseFrame.Time.Name;
                    cmbObj.Text = Selected_Node.CaseFrame.Obj.Name;

                }
                else
                {
                    comboBox1.SelectedIndex = 0;
                    txtScene.Text = Selected_Node.Name;
                }


            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!is_redacted)
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    formMain.SS.AddScene(txtScene.Text);

                }
                if (comboBox1.SelectedIndex == 1)
                    //formMain.SS.AddAction(cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text, X, Y);
                    formMain.SS.AddAction(txtAgent.Text, txtRec.Text, cmbAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text, X, Y);
           
                }
            else
            {
                if (comboBox1.SelectedIndex == 0)
                {
                    if (Selected_Node.TypeN != TypeNode.Scene)
                    {

                        IconNode newn = (IconNode)Selected_DNode;
                        newn.ColOfNode = Color.Red;
                        formMain.diagramView1.Refresh();
                        Selected_Node.TypeN = TypeNode.Scene;
                        //start Ирина (22.03.11)
                        ShapeNode Shnode = formMain.GetShapeNodeById((ClassNode)newn.Id);
                        DiagramLinkCollection allLink = Shnode.GetAllLinks();
                        foreach (DiagramLink item in allLink)
                        {
                            formMain.diagram2.Nodes.Remove(item.Destination);
                            formMain.diagram2.Links.Remove(item);
                        }
                        Shnode.Text = txtScene.Text;
                        newn.Text = "Сцена - " + txtScene.Text;
                        Shnode.ResizeToFitText(FitSize.KeepRatio);
                        newn.ResizeToFitText(FitSize.KeepRatio);
                        //end Ирина (22.03.11)
                    }
                    formMain.SS.ChScene(Selected_Node, txtScene.Text);
                    //start Ирина (22.03.11)
                    ShapeNode Shnode1 = formMain.GetShapeNodeById((ClassNode)((IconNode)Selected_DNode).Id);
                    Shnode1.Text = txtScene.Text;
                    Shnode1.ResizeToFitText(FitSize.KeepRatio);
                    ((IconNode)Selected_DNode).Text = "Сцена - " + txtScene.Text;
                    ((IconNode)Selected_DNode).ResizeToFitText(FitSize.KeepRatio);
                    //end Ирина (22.03.11)
                }
                if (comboBox1.SelectedIndex == 1)
                {
                    if (Selected_Node.TypeN != TypeNode.Action)
                    {
                        if (Selected_Node.LstNodes.Count != 0)
                        {
                            DialogResult ToContineu = MessageBox.Show("Сцена ссылалась на другие вершины, в случае изменения они будут удалены", "Внимание!",
                           MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                            if (ToContineu == DialogResult.Yes)
                            {
                                List<DiagramLink> links_to_remove = new List<DiagramLink>();

                                foreach (DiagramLink item in formMain.diagram1.Links)
                                {
                                    if (item.Origin.Id == Selected_Node)
                                        links_to_remove.Add(item);
                                }
                                Selected_Node.LstNodes.Clear();
                                foreach (DiagramLink item in links_to_remove)
                                {
                                    formMain.diagram1.Links.Remove(item);
                                }
                                links_to_remove.Clear();
                            }
                            else
                            {
                                return;
                            }

                        }
                        IconNode newnn = (IconNode)Selected_DNode;
                        newnn.ColOfNode = Color.Green;
                        formMain.diagramView1.Refresh();
                        

                        Selected_Node.TypeN = TypeNode.Action;
                        Selected_Node.CaseFrame = new ClassCaseFrame();
                        //formMain.SS.ChAction(Selected_Node, cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                        formMain.SS.ChAction(Selected_Node, txtAgent.Text, txtRec.Text, cmbAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                        //start Ирина (22.03.11)
                        Selected_Node.CaseFrame.Agent.X_d2 = X - 30;
                        Selected_Node.CaseFrame.Agent.Y_d2 = Y;
                        Selected_Node.CaseFrame.Recipient.X_d2 = X - 30;
                        Selected_Node.CaseFrame.Recipient.Y_d2 = Y - 5;
                        Selected_Node.CaseFrame.ToDo.X_d2 = X - 30;
                        Selected_Node.CaseFrame.ToDo.Y_d2 = Y - 10;
                        Selected_Node.CaseFrame.Lokotiv.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Lokotiv.Y_d2 = Y;
                        Selected_Node.CaseFrame.Obj.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Obj.Y_d2 = Y - 5;
                        Selected_Node.CaseFrame.Time.X_d2 = X + 30;
                        Selected_Node.CaseFrame.Time.Y_d2 = Y - 10;
                        //start Ирина (22.03.11)
                        Selected_Node.Name = "";
                        Change_to_LookDiagram(Selected_Node);
                        formMain.diagramView2.Refresh();
                        //end Ирина (22.03.11)
                        //end Ирина (22.03.11)

                    }
                    else
                    {

                        //formMain.SS.ChAction(Selected_Node, cmbAgent.Text, cmbRec.Text, txtAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                        formMain.SS.ChAction(Selected_Node, txtAgent.Text, txtRec.Text, cmbAction.Text, cmbLoc.Text, cmbTime.Text, cmbObj.Text);
                    }
                    //start Ирина (22.03.11)
                    ShapeNode Shnode = formMain.GetShapeNodeById(Selected_Node);
                    DiagramLinkCollection allLink = Shnode.GetAllLinks();
                    foreach (DiagramLink item in allLink)
                    {
                        if (item.Destination.GetAllLinks().Count == 1)
                        {
                            formMain.diagram2.Nodes.Remove(item.Destination);
                        }
                        formMain.diagram2.Links.Remove(item);
                    }
                    //formMain.diagram2.Nodes.Remove(Shnode);
                    Change_to_LookDiagram(Selected_Node);
                    //ChangeAdd_to_D2();
                    //end Ирина (22.03.11)
                }

                MessageBox.Show("Вершина успешно редактирована");
            }
            //добавление вершины
            this.Close();
        }

        public void ChangeAdd_to_D2()
        {
            ShapeNode sh_item = formMain.GetShapeNodeById(Selected_Node);
            ShapeNode item_node;
            int i = 0;
            DiagramLink item_link = null, link;
            List<ClassCase> allcase = new List<ClassCase>() { Selected_Node.CaseFrame.Agent, Selected_Node.CaseFrame.Recipient, 
                    Selected_Node.CaseFrame.ToDo, Selected_Node.CaseFrame.Lokotiv, Selected_Node.CaseFrame.Obj, Selected_Node.CaseFrame.Time};
            List<string> allcaseName = new List<string>() { "#агент", "#реципиент", "#действие", "#локатив",
                    "#объект", "#время"};
            foreach (ClassCase item_case in allcase)
            {
                item_node = formMain.GetShapeNodeById_Case(item_case);
                if (item_node != null)
                {
                    foreach (DiagramLink ilink in item_node.GetAllLinks())
                        if (ilink.Origin == sh_item)
                            item_link = ilink;
                }
                if (item_case.Name == "")
                {
                    if (item_node != null)
                        if (item_node.GetAllLinks().Count > 1)
                            formMain.diagram2.Links.Remove(item_link);
                        else
                            formMain.diagram2.Nodes.Remove(item_node);
                }
                else
                {
                    if (item_node!=null)
                        if (item_link != null)
                        {
                            if (item_node.Text != item_case.Name)
                                if (item_node.GetAllLinks().Count > 1)
                                {
                                    formMain.diagram2.Links.Remove(item_link);
                                    item_node.Text = item_case.Name;
                                    
                                }
                                else
                                    item_node.Text = item_case.Name;
                            item_node.ResizeToFitText(FitSize.KeepRatio);
                        }
                        else
                        {//создать связь
                            link = new DiagramLink(formMain.diagram2, sh_item, item_node);
                            link.Text = allcaseName.ElementAt<string>(i);
                            link.AllowMoveEnd = link.AllowMoveStart = false;
                            link.ShadowOffsetX = link.ShadowOffsetY = 0;
                            formMain.diagram2.Links.Add(link);
                        }
                    else
                    {//создать вершину и связь
                        item_node = new ShapeNode(formMain.diagram2);
                        item_node.Id = item_case;
                        item_node.Tag = "ClassCase";
                        item_node.Bounds = new RectangleF(item_case.X_d2, item_case.Y_d2, 10, 5);
                        item_node.Shape = Shapes.Ellipse;
                        item_node.Text = item_case.Name.Trim();
                        item_node.ResizeToFitText(FitSize.KeepRatio);
                        item_node.Brush = new MindFusion.Drawing.SolidBrush(Color.CadetBlue);
                        item_node.ShadowOffsetX = item_node.ShadowOffsetY = 0;
                        formMain.diagram2.Nodes.Add(item_node);
                        link = new DiagramLink(formMain.diagram2, sh_item, item_node);
                        link.Text = allcaseName.ElementAt<string>(i);
                        link.AllowMoveEnd = link.AllowMoveStart = false;
                        link.ShadowOffsetX = link.ShadowOffsetY = 0;
                        formMain.diagram2.Links.Add(link);
                    }
                }
                item_link = null;
                i++;
            }
        }

        //start Ирина (22.03.11)
        public void Change_to_LookDiagram(ClassNode node)
        {
            ShapeNode item_case;
            DiagramLink link;
            ShapeNode item = formMain.GetShapeNodeById(node);
            List<ClassCase> allcase = new List<ClassCase>() { node.CaseFrame.Agent, node.CaseFrame.Recipient, 
                    node.CaseFrame.ToDo, node.CaseFrame.Lokotiv, node.CaseFrame.Obj, node.CaseFrame.Time};
            List<string> allcaseName = new List<string>() { "#агент", "#реципиент", "#действие", "#локатив",
                    "#объект", "#время"};
            int i = 0;
            item.Text = "";
            foreach (ClassCase clcase in allcase)
            {
                //start опять Ирина 28.03
                if (clcase.Name.Trim() != "")
                {
                    item_case = formMain.GetCaseNodeByName(clcase.Name.Trim());
                    if (item_case == null)
                    {
                        item_case = new ShapeNode(formMain.diagram2);
                        item_case.Id = clcase;
                        item_case.Tag = "ClassCase";
                        item_case.Bounds = new RectangleF(clcase.X_d2, clcase.Y_d2, 10, 5);
                        item_case.Shape = Shapes.Ellipse;
                        item_case.Text = clcase.Name.Trim();
                        item_case.ResizeToFitText(FitSize.KeepRatio);
                        item_case.Brush = new MindFusion.Drawing.SolidBrush(Color.CadetBlue);
                        item_case.ShadowOffsetX = item_case.ShadowOffsetY = 0;
                        formMain.diagram2.Nodes.Add(item_case);
                    }
                    //end опять Ирина 28.03
                    link = new DiagramLink(formMain.diagram2, item, item_case);
                    link.Text = allcaseName.ElementAt<string>(i);
                    if (link.Text == "#действие")
                    {
                        formMain.GetIconNodeById(node).Text = "Действие - " + clcase.Name.Trim();
                        formMain.GetIconNodeById(node).ResizeToFitText(FitSize.KeepRatio);
                    }
                        
                    link.AllowMoveEnd = link.AllowMoveStart = false;
                    link.ShadowOffsetX = link.ShadowOffsetY = 0;
                    formMain.diagram2.Links.Add(link);
                }
                else
                {
                    ShapeNode ShNode_Case = formMain.GetShapeNodeById_Case(clcase);
                    formMain.diagram2.Nodes.Remove(ShNode_Case);
                }
                i++;
            }
        }
        //Саша-14.04-редактирование проверка на checkboxes
        private void treeViewAg_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
           
            foreach (TreeNode childNode in treeViewAg.Nodes)
            {
                if (childNode.Checked)
                    childNode.Checked = false;
            }
            e.Node.Checked =true;
            txtAgent.Text = e.Node.Text;
        }

        private void treeViewRec_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            foreach (TreeNode childNode in treeViewRec.Nodes)
            {
                if (childNode.Checked)
                    childNode.Checked = false;
            }
            e.Node.Checked = true;
            txtRec.Text = e.Node.Text;
        }

        private void btnAgents_Click(object sender, EventArgs e)
        {
            treeViewAg.Visible = true;
            treeViewRec.Visible = false;
        }

        private void btnRec_Click(object sender, EventArgs e)
        {
            treeViewRec.Visible = true;
            treeViewAg.Visible = false;
        }

        private void treeViewRec_Leave(object sender, EventArgs e)
        {
            treeViewRec.Visible = false;
        }

        private void treeViewAg_Leave(object sender, EventArgs e)
        {
            treeViewAg.Visible = false;
        }

        private void cmbAgent_Leave(object sender, EventArgs e)
        {
            treeViewAg.Visible = false;
        }

        private void cmbRec_Leave(object sender, EventArgs e)
        {
            treeViewRec.Visible = false;
        }

        private void cmbObj_Enter(object sender, EventArgs e)
        {
            treeViewAg.Visible = false;
            treeViewRec.Visible = false;
        }

        private void cmbLoc_Enter(object sender, EventArgs e)
        {
            treeViewAg.Visible = false;
            treeViewRec.Visible = false;
        }

        private void cmbTime_Enter(object sender, EventArgs e)
        {

            treeViewAg.Visible = false;
            treeViewRec.Visible = false;
        }

        private void txtAction_Enter(object sender, EventArgs e)
        {

            treeViewAg.Visible = false;
            treeViewRec.Visible = false;
        }

        private void comboBox1_Enter(object sender, EventArgs e)
        {

            treeViewAg.Visible = false;
            treeViewRec.Visible = false;
        }

        private void cmbRec_Enter(object sender, EventArgs e)
        {
            treeViewAg.Visible = false;
        }

        private void cmbAgent_Enter(object sender, EventArgs e)
        {
            treeViewRec.Visible = false;
        }
        //Саша-14.04-end
        //end Ирина (22.03.11)
    }
}
